wx.$method={
    // 加入购物车
    addCart(goods_id, pg_id, pgp_id,fn) {
      wx.$api.ajaxGoodsInfosCarts({
        goods_id: goods_id || null,
        pg_id: pg_id || null,
        pgp_id: pgp_id || null
      }).then(response => {
         if(fn!=undefined){
         typeof fn =="function"?fn({code:response.data.code,msg:response.data.msg}):fn({})
         }
      })
    },
    // 分享
    share(title,imgUrl,param={},path){
        const strToJson = function (data) {
          var arr = [];
          for (var name in data) {
            arr.push(encodeURIComponent(name) + "=" + encodeURIComponent(data[name]));
          }
          arr.push(("v=" + Math.random()).replace(".", ""));
          return arr.join("&");
        }
        return {
          title: title ||"椰邻优选",
          imageUrl: imgUrl ||"/image/shares.jpg",
          path: path ? path + '?'+ strToJson(param):"pages/index/index?" + strToJson(param),
        }   
    },
    // 获取当前页面this 
    // type 1 当前页面  type 2 上一个界面  
    getPage(type){
        const   page_list   =  getCurrentPages();
        if(type==1){
          return page_list[page_list.length - type];
        }else
        if(type==2&&page_list.length>1){
          return page_list[page_list.length - type];
        }else{
          return null;
        }
    },
    update_userInfo(e,fn){
      let { nickName, avatarUrl}=e||{};
      wx.$api.updateUser({
        nickname: nickName||'',
        headimgurl:avatarUrl||'',
      }).then(response=>{
            console.log("更新用户信息",response)
        typeof fn == "function" ? fn(response.data||{}) : fn({})
      })
    },
    // 购物车数据设置
  setCarNum(i){
    wx.$api.getCartNums({}).then(response=>{
          console.log("购物车数据",response)
      let i   =  Number(response.data.num)
      if (i > 0) {
        wx.setTabBarBadge({
          index: 2,
          text: response.data.num || 0 + ''
        })
      } else {
        wx.hideTabBarRedDot({
          index: 2,
        })
      } 
    })   
    },
}